<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('用户文档列表')"/>
    <style>
        .fixed-table-toolbar {
            display: none;
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <div class="col-sm-6">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>文件名称：</label>
                                <input name="parentId" type="hidden" id="parentId"/>
                                <input type="text" name="fileName"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" id="search" onclick="$.table.search()"><i
                                        class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                        class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>
            <div class="col-sm-6" style="padding-top: 10px;text-align: right">
                <a class="btn btn-success" id="btnAddFileDict" onclick="addFile()">
                    <i class="fa fa-plus"></i> 新建文件夹
                </a>
                <a class="btn btn-primary single" id="btnUpload" onclick="openFileUpload()">
                    <i class="fa upload"></i> 上传文件
                </a>
            </div>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <div id="myDoc" style="margin-top:15px; display: none"></div>
            <table id="bootstrap-table" style="margin-top:15px"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>

<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('gy:userDoc:add')}]]
    console.log(editFlag)
    var removeFlag = [[${@permission.hasPermi('gy:userDoc:remove')}]];
    var prefix = ctx + "gy/userDoc";


    $(function () {

        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove/{id}",
            uploadUrl: prefix + "/upload",
            //  onClickCell: onClickCell,
            modalName: "",
            columns: [
                {
                    field: 'fileId',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'fileName',
                    title: '文件名称',
                    formatter: function (value, item, index) {
                        console.log(item)
                        if (item.fileType == 0) {
                            return '<a href="#" onclick=getFilesByParentId("' + item.fileId + '","' + item.fileType + '",' + 1 + ');>' + value + '</a>';
                            //"<a href='javascript:void(0);' onclick=\"test('" + p1 + "','" + p2 + "'," + p3 +");\">链接</a>";
                            //or '<a href="#" onclick=test("' + p1+ '","'+ p2+ '",' + p3+ ');>'+链接+'</a>';
                        } else {
                            return value;
                        }

                    }

                },
                {
                    title: '文件类型',
                    field: 'fileType',
                    width: '10',
                    widthUnit: '%',
                    align: "left",
                    formatter: function (value, item, index) {
                        //  console.log(item);
                        if (item.fileType == 0) {
                            return '<i class=" fa fa-folder" style="color: #4e91ff"></i>';
                        } else {
                            if (item.fileExt == "xls") {
                                return '<i class="fa fa-file-excel-o" style="color: #16ab52"></i>';
                            } else if (item.fileExt == "doc") {
                                return '<i class="fa fa-file-word-o" style="color: #0e75ff"></i>';
                            } else if (item.fileExt == "ppt") {
                                return '<i class="fa fa-file-powerpoint-o" style="color: #f07011"></i>';
                            } else if (item.fileExt == "txt") {
                                return '<i class="fa fa-file-text" style="color: #7761fb"></i>';
                            } else if (item.fileExt == "pdf") {
                                return '<i class=" fa fa-file-pdf-o" style="color: #e53933"></i>';
                            } else if (item.fileExt == "zip") {
                                return '<i class=" fa fa-file-zip-o" style="color: #667794"></i>';
                            } else if (item.fileExt == "img") {
                                return '<i class="fa fa-file-image-o " style="color: #4957ff"></i>';
                            }
                            return '<span class="label label-warning">未知文件类型</span>';
                        }
                    }
                },
                {
                    field: 'fileExt',
                    title: '文件扩展名'
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                },

                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {

                        var actions = [];
                        if (row.fileType == 0 || row.fileType == "0") {
                            //文件夹，只能修改名称
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.editCustomer(\'' + row.fileId + '\',400,220)">重命名</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.fileId + '\')">删除</a> ');
                        } else {
                            console.log(row)
                            // actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.editCustomer(\'' + row.fileId + '\',400,220)"><i class="fa fa-edit"></i>重命名</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.fileId + '\')">删除</a> ');
                            actions.push('<a class="btn btn-info btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="downloadFile(\'' + row.filePath + '\',\'' + row.fileName + '\')">下载</a> ');
                            actions.push('<a class="btn btn-warning  btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="openDocShare(\'' + row.fileId + '\',400,220)">分享</a> ');
                        }

                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);


    });

    function downloadFile(url, fileName) {
        var elelink = document.createElement("a");
        elelink.style.display = 'none';
        elelink.target = '_blank';
        elelink.href = url;
        elelink.download = fileName;
        document.body.appendChild(elelink);
        elelink.click();
        document.body.removeChild(elelink);
    }

    function openDocShare(fileId) {

        var callback = function (index, layero) {
            var iframeWin = layero.find('iframe')[0];
            iframeWin.contentWindow.submitHandler(index, layero);
        }

        top.layer.open({
            type: 2,
            area: ['600px', '300px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: "文件分享",
            content: prefix + "/docShare/" + fileId,
            btn: ['创建分享', '关闭'],
            // 弹层外区域关闭
            shadeClose: true,
            yes: callback,
            cancel: function (index) {
                return true;
            },
            success: function () {
                $(':focus').blur();
            }
        });
    }


    function getFilesByParentId(fileId, fileType, ancestors) {
        console.log(ancestors)
        var object = {};

        var data = new FormData();
        data.append("fileId", fileId);
        // $.operate.post(prefix + "/daohang", JSON.stringify(object));
        $.ajax({
            type: "POST",
            url: prefix + "/daohang",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function (result) {
                console.log(result)
                if (result.code == web_status.SUCCESS) {
                    if (fileType == 0 || fileType == "0") {
                        $("#myDoc").show()
                        var bar = '<a href="#" onclick=resetBar();>' + "我的文档 " + '</a>  >  ';
                        var length = result.data.length;
                        for (var i = 0; i < length; i++) {
                            bar += '<a href="#" onclick=getFilesByParentId("' + result.data[i].split(",")[1] + '","' + 0 + '",' + 1 + ');>' + result.data[i].split(",")[0] + '</a>  >  ';
                        }
                        $("#myDoc").html(bar.substring(0, bar.length - 3));
                        $("#parentId").val(fileId);
                        $("#search").click();
                    } else {
                        return;
                    }
                }
            },
            error: function (error) {
                // $.modal.alertWarning("图片上传失败。");
            }
        });

    }

    function resetBar() {
        $("#parentId").val("0");
        $("#myDoc").html("")
        $("#myDoc").hide()
        $("#search").click();
    }

    function openFileUpload() {
        var parentId = "0";
        if ($("#parentId").val() != null && $("#parentId").val() != "") {
            parentId = $("#parentId").val();
        }
        $.modal.open("上传文件", prefix + "/upload/" + parentId, '800', '600');
    }

    function addFile() {
        var parentId = "0";
        if ($("#parentId").val() != null && $("#parentId").val() != "") {
            parentId = $("#parentId").val();
        }
        $.modal.open("新建文件夹", prefix + "/add/" + parentId, '400', '220');
    }
</script>
</body>
</html>
